﻿using DataModels;
using Microsoft.Extensions.DependencyInjection;
using System.Reflection;

namespace SEGIService
{
    public static class ServiceCollectionExtensions
    {
        public static IServiceCollection SEGIAddScheduledTasks(this IServiceCollection services)
        {
            // 获取当前程序集
            var assembly = Assembly.GetExecutingAssembly();

            // 查找所有实现了 IScheduledTask 接口的非抽象类
            var taskTypes = assembly.GetTypes()
                .Where(t => typeof(SEGIIScheduledTask).IsAssignableFrom(t)
                         && !t.IsInterface
                         && !t.IsAbstract)
                .ToList();

            // 注册所有任务
            foreach (var taskType in taskTypes)
            {
                services.AddSingleton(typeof(SEGIIScheduledTask), taskType);
                Console.WriteLine($"已注册定时任务: {taskType.FullName}");
            }

            // 注册后台服务
            services.AddHostedService<ScheduledTaskService>();

            return services;
        }
    }
}
